package module;

import java.awt.List;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import bean.ShopCart;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;

import DateBase.Datebase;

public class ShopCartModule {
	Datebase con=new Datebase();
	Connection conn=(Connection) con.getConnection();
	//get shopcart msg
	public ArrayList getShopCartMsg(int userid) throws SQLException{
		Statement st=(Statement) conn.createStatement();
		String sql="select * from car where userid="+userid+"";
		ResultSet rs=st.executeQuery(sql);
		ArrayList list=new ArrayList();
		while(rs.next()){
			ShopCart shopcart=new ShopCart();
			shopcart.setCarid(rs.getInt("carid"));
			shopcart.setNum(rs.getInt("num"));
			shopcart.setShopid(rs.getInt("shopid"));
			shopcart.setPicture(rs.getString("picture"));
			shopcart.setPrice(rs.getFloat("price"));
			shopcart.setShopname(rs.getString("shopname"));
			list.add(shopcart);
		}
		return list;
	}
	//del action
	public int shopCartDel(int carid) throws SQLException{
		Statement st=(Statement) conn.createStatement();
		String sql="delete from car where carid="+carid+"";
		int flag=st.executeUpdate(sql);
		return flag;
	}
	
	//add and sub action
	public int shopCartChgNum(int carid,int num)throws SQLException{
		Statement st=(Statement) conn.createStatement();
		String sql="update car set num="+num+" where carid="+carid+"";
		int flag=st.executeUpdate(sql);
		return flag;
	}
	
	//pay action [in order to simple But use price is not safe]
	public int shopCartPay(int userid,float price)throws SQLException{
		Statement st=(Statement) conn.createStatement();
		String sql1="insert into ordermsg(userid,price,sta) values("+userid+","+price+",0)";
		int flag1=st.executeUpdate(sql1);
		String sql2="delete from car where userid="+userid+"";
		int flag2=st.executeUpdate(sql2);
		int flag=flag1*flag2;
		return flag;
	}
}
